package com.gao.api.gate.quick.proto.point;

import com.gao.api.gate.quick.core.async.ListenableFutureTask;
import com.gao.api.gate.quick.core.service.ServiceException;
import com.gao.api.gate.quick.proto.http.RequestContext;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * <p></p>
 *
 * @author gaoshuangwen
 * @since 2022/01/18 14:38
 */
public abstract class AbstractEndpoint<E extends Endpoint<?, ?>, C extends EndpointConfig> implements Endpoint<E, C> {

    protected final AtomicBoolean running = new AtomicBoolean();
    private final EndpointHandler handler;
    private final ExecutorService executor;
    private final boolean throwExceptionIfRuning = false;

    protected AbstractEndpoint(EndpointHandler handler, ExecutorService executor) {
        this.handler = handler;
        this.executor = executor;
    }

    @Override
    public E open(C config) {
        if(running.compareAndSet(false, true)) {

          return doOpen(config);

        }else{

        }
        return null;
    }

    @Override
    public Flow transfer(Endpoint<?, ?> target, RequestContext context,Flow flow) {
        if(isRunning()){

        }else{

        }
    }

    @Override
    public boolean isRunning() {
        return running.get();
    }

    @Override
    public EndpointHandler getHandler() {
        return handler;
    }

    @Override
    public ExecutorService loop() {
        return executor;
    }

    abstract E doOpen(C config);

    Flow transfer(Endpoint<?,?> target, RequestContext context);
}
